Skip to content

Expose Management API rate limit info via a client callback - #774

Open
ProdigyTom wants to merge 1 commit into
auth0:masterfrom
ProdigyTom:feat/expose-rate-limit-data
Open

Expose Management API rate limit info via a client callback#774
ProdigyTom wants to merge 1 commit into
auth0:masterfrom
ProdigyTom:feat/expose-rate-limit-data

Conversation

@ProdigyTom

@ProdigyTom ProdigyTom commented Jul 27, 2026

Copy link
Copy Markdown

Changes

Exposes Auth0's rate-limit information (x-ratelimit-limit / -remaining / -reset) from every Management API response, so callers can monitor how close they are to the limit (the ask in #606). It's delivered via an opt-in callback rather than a changed return value — see the note below on why.

  • Auth0::Internal::Http::RateLimit — value object (limit, remaining as Integers; reset as a UTC Time). Blank/non-numeric header values become nil (never a misleading 0).
  • RawClient gains an optional rate_limit_handler, invoked in #send after retries on every response with the parsed RateLimit. Handler errors are swallowed so a monitoring callback can never break a request. #send's return value is unchanged, so the generated callers are unaffected.
  • Wiring: Auth0::Client.new(..., management_rate_limit_handler: ->(rl) { ... }) attaches the handler to the management raw client.
  • All changes live in fernignored files (lib/auth0/internal/**, lib/auth0/mixins/**, lib/auth0/auth_client.rb), so they survive regeneration.
  • Unit tests for RateLimit, RawClient#send handler behavior, and the client wiring.
client = Auth0::Client.new(
  domain: ENV["AUTH0_DOMAIN"],
  token: ENV["AUTH0_MGMT_TOKEN"],
  management_rate_limit_handler: lambda do |rate_limit|
    StatsD.gauge("auth0.mgmt.remaining", rate_limit.remaining) if rate_limit.remaining
  end
)
client.users.get(id: "auth0|123") # handler fires with the response's rate limit

Why a callback instead of returning the rate limit (e.g. with_raw_response)? The generated endpoint methods (Auth0::Users::Client#get, etc.) parse the body and discard the response, and they're regenerated — so a third-party change to what they return wouldn't survive. A callback invoked at the maintained RawClient layer is the part we can change durably while still delivering the data on every request. If you'd prefer to also expose it as a return value via a generated with_raw_response-style accessor, this RateLimit object slots right in — happy to align.

References

Testing

Unit tests under test/unit/ cover header parsing (0 vs blank/non-numeric), the handler firing with the parsed rate limit while the response is returned unchanged, handler errors not breaking the request, and the client wiring. Full rake test passes locally with no failures.

  • This change adds unit test coverage
  • This change adds integration test coverage
  • This change has been tested on the latest version of Ruby

Checklist

  • I have read the Auth0 general contribution guidelines
  • I have read the Auth0 Code of Conduct
  • All existing and new tests complete without errors
  • Rubocop passes on all added/modified files
  • All active GitHub checks have passed

@ProdigyTom
ProdigyTom requested a review from a team as a code owner July 27, 2026 18:57
@kishore7snehil

Copy link
Copy Markdown
Contributor

@ProdigyTom I can see some merge conflicts. Can you please resolve them? Post that I can take a look

Exposes Auth0's rate limit info (x-ratelimit-limit / -remaining / -reset) from
every Management API response via an opt-in callback, so callers can monitor how
close they are to the limit (per auth0#606).

- Add Auth0::Internal::Http::RateLimit (limit/remaining/reset; blank and
  non-numeric header values become nil rather than 0)
- RawClient gains a rate_limit_handler, invoked in #send after retries on every
  response; handler errors are swallowed so they can't break a request. #send's
  return value is unchanged, so generated callers are unaffected.
- Wire it through the custom client: Auth0::Client.new(management_rate_limit_handler:)
  attaches the handler to the management raw client
- Unit tests for RateLimit, RawClient#send handler behavior, and client wiring

All changes live in fernignored files, so they survive regeneration. Refs auth0#606.
@ProdigyTom
ProdigyTom force-pushed the feat/expose-rate-limit-data branch from 1943366 to f119645 Compare September 2, 2026 13:39
@ProdigyTom ProdigyTom changed the title Expose rate limit data via optional block on API responses Expose Management API rate limit info via a client callback Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants